package com.ruoyi.common.utils;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class NameUtils {
    public static <T> void handleUserNameToNickName(List<T> list, List<SysUser> sysUsers, String... fieldNames) {
        list.forEach(item -> {
            for (String fieldName : fieldNames) {
                try {
//                    Field declaredField = item.getClass().getDeclaredField(fieldName);
                    Field declaredField = getField(item.getClass(),fieldName);
                    declaredField.setAccessible(true);
                    if ("notifier".equals(fieldName)) {
                        String notifier = (String) declaredField.get(item);
                        if (StringUtils.isNotBlank(notifier)) {
                            declaredField.set(item, Arrays.stream(notifier.split(","))
                                    .map(s -> sysUsers.stream()
                                            .filter(u -> u.getUserName().equals(s))
                                            .map(SysUser::getNickName)
                                            .findFirst()
                                            .orElse(""))
                                    .collect(Collectors.joining(",")));
                        }
                    } else {
                        String userName = (String) declaredField.get(item);
                        declaredField.set(item, sysUsers.stream()
                                .filter(s -> s.getUserName().equals(userName))
                                .map(SysUser::getNickName)
                                .findFirst()
                                .orElse(""));
                    }
                } catch (NoSuchFieldException e) {
                    // 处理字段不存在的情况
                    continue; // 跳过处理下一个字段
                } catch (IllegalAccessException | IllegalArgumentException e) {
                    // Handle other exceptions if needed
                }
            }
        });
    }

    public static <T> void handleUserNameToNickName(List<T> list, List<SysUser> sysUsers, String[] userNameFieldNames,String[] nickNameFieldNames) {
        if (userNameFieldNames.length != nickNameFieldNames.length){
            throw  new ServiceException("用户名称字段集合和用户昵称字段集合长度不一致");
        }
        list.forEach(item -> {
            for (int i = 0; i < userNameFieldNames.length; i++) {
                String userNameFieldName = userNameFieldNames[i];
                String nickNameFieldName = nickNameFieldNames[i];
                try {
                    Field userNameField = getField(item.getClass(),userNameFieldName);
                    Field nickNameField = getField(item.getClass(),nickNameFieldName);
                    userNameField.setAccessible(true);
                    nickNameField.setAccessible(true);
                    String userName = (String) userNameField.get(item);
                    if (StringUtils.isNotBlank(userName)) {
                        nickNameField.set(item, Arrays.stream(userName.split(","))
                                .map(s -> sysUsers.stream()
                                        .filter(u -> u.getUserName().equals(s))
                                        .map(SysUser::getNickName)
                                        .findFirst()
                                        .orElse(""))
                                .collect(Collectors.joining(",")));
                    }
                } catch (NoSuchFieldException e) {
                    // 处理字段不存在的情况
                    continue; // 跳过处理下一个字段
                } catch (IllegalAccessException | IllegalArgumentException e) {
                    // Handle other exceptions if needed
                }
            }
        });
    }
    private static Field getField(Class<?> clazz,String fieldName) throws NoSuchFieldException {
        Field[] fields = getAllFields(clazz);
        List<Field>  fields1 = Arrays.stream(fields).filter(item->{
            return item.getName().equals(fieldName);
        }).collect(Collectors.toList());
        if(fields1.size() > 0) return fields1.get(0);
        throw new NoSuchFieldException();
    }
    private static Field[] getAllFields(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        Class<?> parent = clazz.getSuperclass();
        if (parent != null) {
            Field[] parentFields = getAllFields(parent);
            Field[] allFields = new Field[fields.length + parentFields.length];
            System.arraycopy(fields, 0, allFields, 0, fields.length);
            System.arraycopy(parentFields, 0, allFields, fields.length, parentFields.length);
            fields = allFields;
        }
        return fields;
    }
}
